﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AAA.WebPublisher
{
    public abstract class AbstractPublisher : IPublisher, IRegister
    {
/*
        protected const int LOGIN = 1;
        protected const int LOGOUT = 2;
        protected const int PICTURE_VALIDATE = 3;
        protected const int UPLOAD_PICTURE = 4;
        protected const int POST_ARTICLE = 5;
*/
        protected const int REDIRECT_TO_LOGIN = 0;
        protected const int LOGIN_FORM = 1;
        protected const int REDIRECT_TO_BLOG_HOME = 20;
        protected const int REDIRECT_TO_BLOG = 21;
        protected const int LOGIN_COMPLETED = 3;
        protected const int FILL_BLOG = 4;
        protected const int PUBLISH = 5;
        protected const int POST_COMPLETED = 6;
        protected const int LOGOUT = 7;

        protected const int REDIRECT_TO_REGISTER = 101;
        protected const int REGISTER_FORM = 102;
        protected const int REGISTER_COMPLETED = 103;
        protected const int COMPLETED_CONFIRM = 104;
        protected const int MAIL_CONFIRM = 105;
        protected const int REDIRECT_TO_FIRST_BLOG = 106;
        protected const int REDIRECT_TO_BLOG_CONFIRM = 107;
        protected const int BLOG_CONFIRM_FORM = 108;
        protected const int BLOG_CONFIRM_COMPLETED = 109;
        protected const int BLOG_EMAIL_CONFIRM = 110;
        protected const int BLOG_MOBILE_CONFIRM = 111;
        protected const int INPUT_CONFIRM_CODE = 112;
                
        protected const int CREATE_BLOG_LOGIN = 201;
        protected const int CREATE_BLOG_LOGIN_FORM = 202;
        protected const int REDIRECT_TO_CREATE_BLOG = 203;
        protected const int CREATE_BLOG = 204;
        protected const int CREATE_BLOG_FORM = 205;
        protected const int CREATE_BLOG_FORM2 = 206;
        protected const int CREATE_BLOG_FORM3 = 207;
        protected const int CREATE_BLOG_FORM4 = 208;

        protected const int REDIRECT_TO_HOMEPAGE = 400;

        private string _strWebSite;
        private string _strWebSiteName;
        private string _strErrorMessage;
        private string _strUsername;
        private string _strPassword;
        private string _strBlogname;
        private bool _needPictureValidate;
        private bool _isLoginCompleted;

        private bool _isRegisterReleased;
        private bool _isPublisherReleased;

        private string _strTitle;
        private string _strArticle;       

        private Dictionary<string, string> _dicRegisterInfo;

        public Dictionary<string, string> RegisterInfo
        {
            get { return _dicRegisterInfo; }
            set { _dicRegisterInfo = value; }
        }

        public bool IsRegisterReleased
        {
            get { return _isRegisterReleased; }
            set { _isRegisterReleased = value; }
        }

        public bool IsPublisherReleased
        {
            get { return _isPublisherReleased; }
            set { _isPublisherReleased = value; }
        }

        protected string Title
        {
            get { return _strTitle; }
            set { _strTitle = value; }
        }

        protected string Article
        {
            get { return _strArticle; }
            set { _strArticle = value; }
        }

        public bool LoginCompleted
        {
            get { return _isLoginCompleted; }
            set { _isLoginCompleted = value; }
        }
        private WebBrowser _webBrowser;

        public AbstractPublisher()
        {
            _webBrowser = new WebBrowser();                 
            _webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_webBrowser_DocumentCompleted);
        }

        void _webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
                ParseHtml(_webBrowser.Document);
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message + "," + ex.StackTrace;
            }
        }

        void Window_Error(object sender, HtmlElementErrorEventArgs e)
        {
            e.Handled = true;            
        }

        public WebBrowser WebBrowser
        {
            get
            {
                return _webBrowser;
            }

            set
            {
                _webBrowser = value;
            }
        }

        #region IPublisher Members

        public string WebSite
        {
            get
            {
                return _strWebSite;
            }
            set
            {
                _strWebSite = value;
            }
        }

        public string WebSiteName
        {
            get
            {
                return _strWebSiteName;
            }
            set
            {
                _strWebSiteName = value;
            }
        }

        public string ErrorMessage
        {
            get
            {
                return _strErrorMessage;
            }
            set
            {
                _strErrorMessage = value;
            }
        }

        public string Username
        {
            get
            {
                return _strUsername;
            }
            set
            {
                _strUsername = value;
            }
        }

        public string Password
        {
            get
            {
                return _strPassword;
            }
            set
            {
                _strPassword = value;
            }
        }

        public string Blogname
        {
            get
            {
                return _strBlogname;
            }
            set
            {
                _strBlogname = value;
            }
        }


        public bool NeedPictureValidate
        {
            get
            {
                return _needPictureValidate;
            }
            set
            {
                _needPictureValidate = value;
            }
        }

        protected bool ParseFile(string strFilename)
        {
            switch (strFilename.Substring(strFilename.LastIndexOf('.') + 1))
            {
                case "txt":
                    return ParseTextFile(strFilename);

                case "htm":
                case "html":
                    return ParseHtmlFile(strFilename);
                    
            }
            return false;
        }

        private bool ParseHtmlFile(string strFilename)
        {

            HtmlAgilityPack.HtmlDocument document;
            FileStream fileStream = null;
            byte[] bData = null;
            MemoryStream ms = null;            

            try
            {

                fileStream = new FileStream(strFilename, FileMode.Open);
                bData = new byte[fileStream.Length];
                fileStream.Read(bData, 0, bData.Length);

                ms = new MemoryStream(bData);
                document = new HtmlAgilityPack.HtmlDocument();                
                document.Load(ms, Encoding.Default);

                _strTitle = strFilename.Substring(strFilename.LastIndexOf("\\") + 1);
                _strTitle = _strTitle.Substring(0, _strTitle.IndexOf('.'));

                _strArticle = document.DocumentNode.SelectSingleNode("html").InnerHtml;

                return true;

            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message + "," + ex.StackTrace;
            }
            return false;
        }

        private bool ParseTextFile(string strFilename)
        {
            StreamReader sr = null;
            string strLine;
            try
            {

                sr = new StreamReader(strFilename, Encoding.Default);

                _strTitle = sr.ReadLine();
                _strArticle = "";
                while ((strLine = sr.ReadLine()) != null)
                {
                    _strArticle += strLine + "<br>";
                }

                return true;

            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message + "," + ex.StackTrace;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return false;
        }

        protected abstract bool ParseHtml(HtmlDocument document);
        public abstract bool Login();
        public abstract bool Logout();
        public abstract bool PictureValidate();
        public abstract bool UploadPicture(string strPictureName);
        public abstract bool PostArticle(string strFilename);

        #endregion

        #region IRegister Members

        public abstract bool ReadConfig(string strConfig);
        public abstract bool Register();
        public abstract bool CreateBlog();
        #endregion
    }
}
